package com.sgb.kjwl.view.ui.activity.self;

import android.content.Context;
import android.content.Intent;
import androidx.databinding.DataBindingUtil;
import androidx.recyclerview.widget.LinearLayoutManager;
import android.view.View;

import com.sgb.kjwl.AppActivity;
import com.sgb.kjwl.R;
import com.sgb.kjwl.databinding.ActivityCarManagerBinding;
import com.sgb.kjwl.di.self.DaggerSelfComponent;
import com.sgb.kjwl.di.self.SelfModule;
import com.sgb.kjwl.model.entity.OilInfoEntity;
import com.sgb.kjwl.view.ui.adapter.self.OilListAdapter;
import com.sgb.kjwl.viewmodel.self.OilListModel;
import com.swgk.core.base.di.AppComponent;
import com.swgk.core.recyclerview.decoration.RecyclerViewItemDecoration;
import com.swgk.core.xrecyclerview.XRecyclerView;

import java.util.List;

import javax.inject.Inject;

public class GasTypeListActivity extends AppActivity {
    ActivityCarManagerBinding binding;
    OilListAdapter adapter;
    @Inject
    OilListModel model;

    @Override
    protected void initView() {
        binding = DataBindingUtil.setContentView(this, R.layout.activity_car_manager);
        binding.setViewModel(model);
        binding.include.rightView.setText("新增");
        binding.include.titleView.setText("油品管理");
        binding.include.rightView.setVisibility(View.VISIBLE);
        binding.include.rightView.setOnClickListener(this);
        binding.include.ivBack.setOnClickListener(this);
        binding.recyclerView.addItemDecoration(new RecyclerViewItemDecoration(getResources().getDimension(R.dimen.dp10)));
        LinearLayoutManager layoutManager = new LinearLayoutManager(getActivity());
        binding.recyclerView.setLayoutManager(layoutManager);
        binding.recyclerView.setLoadingMoreEnabled(true);
        binding.recyclerView.setLoadingListener(new XRecyclerView.LoadingListener() {
            @Override
            public void onRefresh() {
                model.getOilList(false);
            }

            @Override
            public void onLoadMore() {
                model.getOilList(true);
            }
        });
        adapter=new OilListAdapter(getActivity(),null);
        adapter.setListener(new OilListAdapter.clickButtonListener() {
            @Override
            public void putAway(int pos, int id,int type) {
                model.updateStatus(id,type);

            }

            @Override
            public void edite(OilInfoEntity oilInfoEntity) {
                EditOilActivity.start(getActivity(),oilInfoEntity);
             //   EventBus.getDefault().post(LeaseEvent.make().setMessageTag(EventConstant.EVENT_OILINFO).setMessageData(oilInfoEntity));
            }
        });
        binding.recyclerView.setAdapter(adapter);

    }
    public static void start(Context context) {
        Intent intent = new Intent(context, GasTypeListActivity.class);

        context.startActivity(intent);
    }
    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.iv_back:
                finish();
                break;
            case R.id.right_view:

                EditOilActivity.start(getActivity());
                break;
        }
    }
    @Override
    protected void init() {

    }

    @Override
    protected void onResume() {
        super.onResume();
        model.getOilList(false);
    }

    @Override
    protected void setupActivityComponent(AppComponent appComponent) {
        DaggerSelfComponent.builder().appComponent(appComponent).selfModule(new SelfModule(this)).build().inject(this);
    }
    public void getList(List<OilInfoEntity> list,boolean isLoadMore){
        binding.recyclerView.refreshComplete();
        if (isLoadMore) {
            binding.recyclerView.loadMoreComplete();
            if (list.size() > 0) {
                adapter.addDatas(list);
            } else {
                binding.recyclerView.setLoadingMoreEnabled(false);
            }
        } else {
            binding.recyclerView.refreshComplete();
            if (list.size() > 0) {
                binding.emptyView.setVisibility(View.GONE);
                binding.recyclerView.setVisibility(View.VISIBLE);
                adapter.setDatas(list);
                binding.recyclerView.setLoadingMoreEnabled(true);
            } else {
                adapter.setDatas(null);
                binding.emptyView.setVisibility(View.VISIBLE);
                binding.recyclerView.setVisibility(View.GONE);
            }
        }

    }
}
